package com.woniuxy.hotel.setting.handler;

import com.woniuxy.hotel.setting.service.QueryService;
import com.woniuxy.hotel.setting.service.SettingService;
import com.woniuxy.hotel.setting.util.TranslateUtil;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: desk-----xuzhiliang
 * @Date: 2019/8/9 17:01
 * @Description:
 */
@Controller
public class DownloadHandler {
    @RequestMapping("/test")
    public ModelAndView test(){
        Subject currentUser = SecurityUtils.getSubject();
		String name = (String) currentUser.getPrincipal();
        ModelAndView modelAndView = new ModelAndView("/justForBoss");
        modelAndView.addObject("name",name);
        Map map = queryService.returnAnalyizeData();
        modelAndView.addObject("orders",map.get("orders"));
        modelAndView.addObject("isTrue",false);
        return modelAndView;
    }
    @Autowired
    public SettingService settingService;
    @Autowired
    public QueryService queryService;
    @RequestMapping("/downloadUserInfo")
    //freemarker的模板的转化和二进制文件的下载的实现
    public ResponseEntity<byte[]> downloadUserInfo(HttpServletRequest httpServletRequest , HttpServletResponse httpServletResponse){
        Map map = new HashMap();
        map = queryService.returnAnalyizeData();
        String templateFilename = "downloadUserInfo.ftl";
        String html = "downloadUserInfo.html";
        String xhtml = "downloadUserInfo.xhtml";
        String pdf = "downloadUserInfo.pdf";
        String addr="G:\\repossitory\\ssmsd\\src\\main\\resources\\";
        //生成模板文件downloadUserInfo.ftl/HotelManagemenSystemBasedonJavaWeb/src/main/resources/templates/downloadUserInfo.ftl
        //可以直接返回指定write流/HotelManagemenSystemBasedonJavaWeb/src/main/resources/temp/downloadUserInfo.html
        TranslateUtil.freemarkToHtml(addr+"templates\\",templateFilename,map,addr+"temp\\"+html);
        //html转化到xhtml
        TranslateUtil.html2Xhtml(addr+"temp\\"+html,addr+"temp\\"+xhtml);
        try {
            TranslateUtil.XHtml2Pdf(addr+"temp\\"+xhtml,addr+"temp\\"+pdf);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        //读取模板文件------》执行下载
        File file = new File(addr+"temp\\"+pdf);
        byte[] body = null;
        InputStream is = null;
        try {
            is = new FileInputStream(file);
            if (is.available()==0){
                return null;
            }
            body = new byte[is.available()];
            is.read(body);
        } catch (Exception e) {
            e.printStackTrace();
        }
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", "attchement;filename=" + file.getName());
        HttpStatus statusCode = HttpStatus.OK;
        ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);
        httpServletResponse.setContentType("application/octet-stream");
        try {
            httpServletRequest.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return entity;
    }
}
